package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: MyRabbitConfig
 * Package： com.atguigu.gulimall.ware.config
 * Description:
 *
 * @Author: Sweng
 * @Create: 2025/4/13 - 19:03
 * VERSION: version1.0
 **/
@Configuration
public class MyRabbitConfig {

    @Autowired
    private  RabbitTemplate rabbitTemplate;


    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock-event-exchange",true,false);
    }

    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue",true,false,false,null);
    }


    @Bean
    public Queue stockDelayQueue(){

        Map<String,Object> args = new HashMap<>();
        //死信队列交换机
        args.put("x-dead-letter-exchange","stock-event-exchange");

        //队列
        args.put("x-dead-letter-routing-key","stock.release");

        args.put("x-message-ttl",60000);

        return new Queue("stock.delay.queue",true,false,false,args);
    }
    @Bean
    public Binding stockReleaseBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);
    }

    @Bean
    public Binding stockDelayBinding() {
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null);
    }

    /**
     * 定制RabbitTemplate
     * 1. 服务器收到消息就回调
     *      1.spring。rabbitmq.publisher-returns = true
     *      2.设置确认回调 ConfirmCallback
     * 2. 消息正确抵达队列进行回调
     *      1. spring.rabbitmq.publisher-returns = true
     *         spring.rabbitmq.template.mandatory = true
     *      2. 设置返回结果回调 ReturnCallback
     *
     * 3. 消费端确认回调（保证每个消息正确）
     *      spring.rabbitmq.listener.simple.acknowledge-mode = manual 手动签收
     *      1.默认是自动ack。消息收到。客户端自动确认。服务器移除这个消息
     *
     *      2.如何签收
     *          channel.basicAck(tag,false); 签收 业务完成进行签收
     *          channel.basicNack(tag,false,true) 拒绝 业务失败
     *
     *
     */
    @PostConstruct
    public void initRabbitTemplate(){

        //设置确定 回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){
            //1.消息抵达broker 就ack=true
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("消息发送成功");
            }
        });

        //设置消费抵达队列  回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("Fail Message");
            }
        });

    }

}
